﻿#region Copyright

// All Material Copyright © 2010 Dynamic Concepts Development Corp., All Rights Reserved.
//      Portions Copyright © 1984-2009 Dynamic Concepts Development Corp., All Rights Reserved.
// For further information please contact: SoftwareSupport@dynconcepts.com
// Dynamic Concepts Development Corp.  311 W 57th St. Suite #422, New York, NY 10019

#endregion

#region Using References

using System;
using System.Windows.Threading;

#endregion

namespace DynConDevCorp.OpenArch.SupportLibrary.ParallelExecution
{
    internal class VirtualSyncActionExecutionContext<TPARAM> : VirtualSyncExecutionContextBase
    {
        #region Constructors

        protected VirtualSyncActionExecutionContext(Object owner, Dispatcher owningDispatcher, Dispatcher executingDispatcher, ExecutionRoutineDelegate executionDelegate)
            : base(owner, owningDispatcher, executingDispatcher)
        {
            r_ExecutionDelegate = executionDelegate;
        }

        #endregion

        #region Public Methods

        public static void Execute(Object owner, Dispatcher executingDispatcher, ExecutionRoutineDelegate executionDelegate, TPARAM param)
        {
            VirtualSyncActionExecutionContext<TPARAM> instance = new VirtualSyncActionExecutionContext<TPARAM>(owner, Dispatcher.CurrentDispatcher, executingDispatcher, executionDelegate);
            instance.Execute(param);
            return;
        }

        #endregion

        #region Public Delegates

        public delegate void ExecutionRoutineDelegate(TPARAM param);

        #endregion

        #region Protected Methods

        protected override void InvokeProvidedDelegate()
        {
            r_ExecutionDelegate(m_Param);
        }

        #endregion

        #region Private Methods

        private void Execute(TPARAM param)
        {
            m_Param = param;
            DispatcherOperation operation = ExecuteDispatcherFrame();
            return;
        }

        #endregion

        #region Instance Fields (ReadOnly)

        private readonly ExecutionRoutineDelegate r_ExecutionDelegate;

        #endregion

        #region Instance Fields (Mutable)

        private TPARAM m_Param;

        #endregion
    }
}